package functions.ems_claim;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;

import com.beans.AdminUserBean;
import com.beans.CityBean;
import com.beans.ClaimBean;
import com.beans.CompanyBean;
import com.beans.FormBean;
import com.beans.ORGBean;
import com.beans.ProjectBean;
import com.beans.TravelStandard;
import com.beans.VEHICLE;
import com.crc.mam.BaseCase;
import coms.Constants;
import frame.HttpHelper;
import functions.mams_rest.Org;
import functions.mams_rest.User;

public class ClaimTravelInfo extends BaseCase{
	
	private org.slf4j.Logger logger = LoggerFactory.getLogger(ClaimTravelInfo.class);

	
	 String subcategoryCode;
	 String subCategoryName;
	CityBean startCity;
	CityBean desCity;
    ClaimBean responseBean;
    TravelStandard travel;
    ProjectBean selectProject;
    VEHICLE selectVehicle;
    VEHICLE selectLevel;
    ORGBean applyOrg;
    String claimId;
    String amount;
    String applyCompany;


	public String getApplyCompany() {
      return applyCompany;
    }


    public void setApplyCompany(String applyCompany) {
      this.applyCompany = applyCompany;
    }


  public String getAmount() {
      return amount;
    }


    public void setAmount(String amount) {
      this.amount = amount;
    }


  public String getResult() {
		return result;
	}


	public void setResult(String result) {
		this.result = result;
	}


	

	public String getSubcategoryCode() {
		return subcategoryCode;
	}


	public void setSubcategoryCode(String subcategoryCode) {
		this.subcategoryCode = subcategoryCode;
	}


	public String getSubCategoryName() {
		return subCategoryName;
	}


	public void setSubCategoryName(String subCategoryName) {
		this.subCategoryName = subCategoryName;
	}


	public CityBean getStartCity() {
		return startCity;
	}


	public void setStartCity(CityBean startCity) {
		this.startCity = startCity;
	}


	public CityBean getDesCity() {
		return desCity;
	}


	public void setDesCity(CityBean desCity) {
		this.desCity = desCity;
	}


	public ClaimBean getResponseBean() {
		return responseBean;
	}


	public void setResponseBean(ClaimBean responseBean) {
		this.responseBean = responseBean;
	}


	public TravelStandard getTravel() {
		return travel;
	}


	public void setTravel(TravelStandard travel) {
		this.travel = travel;
	}


	public ProjectBean getSelectProject() {
		return selectProject;
	}


	public void setSelectProject(ProjectBean selectProject) {
		this.selectProject = selectProject;
	}


	public VEHICLE getSelectVehicle() {
		return selectVehicle;
	}


	public void setSelectVehicle(VEHICLE selectVehicle) {
		this.selectVehicle = selectVehicle;
	}


	public VEHICLE getSelectLevel() {
		return selectLevel;
	}


	public void setSelectLevel(VEHICLE selectLevel) {
		this.selectLevel = selectLevel;
	}


	public ORGBean getApplyOrgOrg() {
		return applyOrg;
	}


	public void setApplyOrg(ORGBean applyOrg) {
		this.applyOrg = applyOrg;
	}


	public String getClaimId() {
		return claimId;
	}


	public void setClaimId(String claimId) {
		this.claimId = claimId;
	}


	/**插入报账明细
	 * 
	 * @param overStandard
	 * insertUrl="/ems-claim/claimTravelInfo/insert"
	 */
	public void insert(boolean overStandard) {
		/*
		 * int tmp = 1; System.out.println(travel.getHotelStandard()); int standard =
		 * (int)travel.getHotelStandard(); System.out.println(standard);
		 * if(overStandard) { tmp = standard + 100; }else { Random dom = new
		 * Random(standard); tmp = dom.nextInt(standard-1); }
		 */
		logger.info("报账单申请---插入报账明细 ------------------ start---------------");
		int standard = (int) travel.getHotelStandard();
		int tmp = standard;
		this.amount = String.valueOf(tmp);
		//int tmp = 110;

		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = formatter.format(currentTime);

		Calendar now = Calendar.getInstance();
		now.setTime(currentTime);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + 4);
		String endDate = formatter.format(now.getTime());

		String claimId = "";
		String applEmpName = "";
		String applJobName = "";
		String applOrgDeptName = "";
		String applEmpNo = "";

		if (responseBean == null) {
		//	claimId = PropertiesUtil.getValue("claim", "claimId");
			if (claimId == null) {
				claimId = "";
			}
		//	applEmpName = PropertiesUtil.getValue("claim", "applEmpName");
			if (applEmpName == null) {
				applEmpName = "";
			}
			//applJobName = PropertiesUtil.getValue("claim", "applJobName");
			if (applJobName == null) {
				applJobName = "";
			}
		//	applOrgDeptName = PropertiesUtil.getValue("claim", "applOrgDeptName");
			if (applOrgDeptName == null) {
				applOrgDeptName = "";
			}
		//	applEmpNo = PropertiesUtil.getValue("claim", "applEmpNo");
			if (applEmpNo == null) {
				applEmpNo = "";
			}
		} else {
			claimId = responseBean.getClaimId();
			applEmpName = responseBean.getApplEmpName();
			applJobName = responseBean.getApplJobName();
			applOrgDeptName = responseBean.getApplOrgDeptName();
			applEmpNo = responseBean.getApplEmpNo();
		}
		User user = new User();
		user.admin();
		AdminUserBean bean = user.getUserBean();
		Org org = new Org();
		applyCompany = bean.getCompanyDesc();
		applyOrg = org.getRandomOrg(applyCompany, bean.getDepartment());
		logger.info("随机选取一个部门 ： " + applyOrg.toString());
		String params = "{\"claimId\":\"" + claimId + "\",\"empName\":\"" + applEmpName + "\",\"jobCode\":\""
				+ applJobName + "\",\"feeDeptName\":\"" + applyOrg.getTitle() + "\",\"feeDeptCode\":\"" + applyOrg.getId()
				+ "\",\"subcategoryCode\":\"" + subcategoryCode + "\",\"subCategoryName\":\"" + subCategoryName
				+ "\",\"startCity\":" + startCity.getCityId() + ",\"endCity\":" + desCity.getCityId()
				+ ",\"currency\":\"CNY\",\"currencyName\":\"人民币\",\"airCode\":\"\",\"startCityName\":\""
				+ startCity.getCityName() + "\",\"endCityName\":\"" + desCity.getCityName()
				+ "\",\"cityTypeDisplay\":\"" + desCity.getCityTypeDisplay() + "\",\"projectCode\":\""
				+ selectProject.getProjectCode() + "\",\"projectName\":\"" + selectProject.getProjectName()
				+ "\",\"areaType\":\"" + desCity.getCityType()
				+ "\",\"transportStandard\":\"飞机:商务舱 | 高铁/动车:一等座 | 火车:软卧 | 轮船:一等舱\",\"startDate\":\"" + startDate
				+ "\",\"endDate\":\"" + endDate + "\",\"applyHotelStandard\":" + tmp + ",\"hotelStandard\":" + standard
				+ ",\"goTransport\":\"" + selectVehicle.getVsiKey() + "\",\"goTransportName\":\""
				+ selectVehicle.getVsiValue() + "\",\"backTransport\":\"" + selectVehicle.getVsiKey()
				+ "\",\"backTransportName\":\"" + selectVehicle.getVsiValue() + "\",\"goApplyLevel\":\""
				+ selectLevel.getVsiKey() + "\",\"backApplyLevel\":\"" + selectLevel.getVsiKey()
				+ "\",\"remark\":\"\"}";
		logger.info("insert params : " +params );
		//System.out.println("insert params --- " + params);
		String url = Constants.INSERT_URL;
		try {
			result = HttpHelper.post(params, url);
			//System.out.println("insert--------" + result);
			logger.info("insert--------" + result);
			status = HttpHelper.isOk;
			if(status) {
			  
			}else {
			  try {
			    JSONObject object = new JSONObject(result);
			    remark = object.getString("message");
			  }catch(JSONException e) {
			    remark = "JSON解析错误";
			  }
			}
		} catch (Exception e) {
		  status = false;
		  remark = "系统异常";
			e.printStackTrace();
		}
		logger.info("---插入报账明细 ------------------ end------------------------");
	}
	
	
	/**
	 * 插入明细之后，通过claimId查询
	 * findListUrl="/ems-claim/claimTravelInfo/findList"
	 */
	public void findList() {
		logger.info("插入明细之后，通过"+claimId+ "查询报账明细行 ----- start -------" );
		String params = "{\"claimId\":" + claimId + "}";
		String url = Constants.FIND_LIST_URL;
		try {
			result = HttpHelper.post(params, url);
			status = HttpHelper.isOk;
			logger.info("findList--------" + result);
			//System.out.println("findList--------" + result);
			if(status) {
			  try {
			  JSONArray array = new JSONArray(result);
	            int total = array.length();
	            if(total > 0 ) {
	                
	            }else {
	              status = false;
	              remark = "没有找到对应的"+claimId+"报账单";
	              logger.error("没有找到对应的"+claimId+" 报账单");
	            }
			  }catch(JSONException e) {
			    remark = "JSON解析错误";
			    logger.error("JSON解析错误");
			  }
			}else {
			  try {
			    JSONObject object = new JSONObject(result);
			    remark = object.getString("message");
			  }catch(JSONException e) {
			    remark = "JSON解析错误";
			    logger.error("JSON解析错误");
			  }
			  
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			remark = "系统异常";
			status = false;
		}
		logger.info("插入明细之后，通过"+claimId+ "查询报账明细行 ----- end -------" );
	}
}
